package com.bawei.vehicle

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.bean.AddVehiclesEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.vehicle.databinding.ActivityVehicleBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/vehicle/VehicleActivity")
class VehicleActivity : BaseActivity<ActivityVehicleBinding,VehicleViewModel>() {
    lateinit var vehicleAdapter: VehicleAdapter
    override fun initView() {
        viewModel.getProvinces()
        vehicleAdapter = VehicleAdapter()
        binding.carRec.adapter=vehicleAdapter
        binding.carRec.layoutManager=GridLayoutManager(this,7)
        vehicleAdapter.setOnItemClickListener { _, _, position ->
            binding.carArea.text=vehicleAdapter.data[position].abbreviation
        }
        binding.addCar.setOnClickListener {
                var addVehiclesEntity=AddVehiclesEntity()
            addVehiclesEntity.apply {
                licensePlate=binding.carArea.text.toString()+binding.carNumber.text.toString()
                provinceAbbreviation=binding.carArea.text.toString()
                viewModel.addVehicles(addVehiclesEntity)
            }
        }
    }

    override fun initData() {
        viewModel.provincesSuccess.observe(this){
            vehicleAdapter.data.clear()
            vehicleAdapter.data.addAll(it)
            vehicleAdapter.notifyDataSetChanged()
        }
        viewModel.addvehiclesSuccess.observe(this){
            ToastUtils.showLong("添加成功")
        }
    }
}